#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT(strix, 0.1, mizutani@sfc.wide.ad.jp)
AM_INIT_AUTOMAKE(strix, 0.1, mizutani@sfc.wide.ad.jp)
AC_CONFIG_SRCDIR([src/main.cpp])

AC_CONFIG_FILES([Makefile
                 src/Makefile
		 src/util/Makefile
		 src/netdec/Makefile])


AC_CHECK_HEADERS([netdb.h stdlib.h string.h])
AC_HEADER_STDBOOL
AC_C_INLINE

# Checks for programs.
AC_PROG_RANLIB
AC_PROG_CXX
AC_PROG_CC

CXXFLAGS="$CXXFLAGS -Wall"
	      
AC_ARG_ENABLE(debug,
[  --enable-debug          Enable debug options (for developers)],
        [ if test "$enable_debug" = "yes" ; then
	    if test -n "$GCC"; then
	      CXXFLAGS=`echo $CXXFLAGS | sed -e "s/\-O2//g"`
              CXXFLAGS="$CXXFLAGS -DENABLE_DEBUG -g -O0"
	      CFLAGS=`echo $CFLAGS | sed -e "s/\-O2//g"`
              CFLAGS="$CFLAGS -DENABLE_DEBUG -g -O0"
            fi
	  fi
        ],)

AC_ARG_ENABLE(profile,
[  --enable-profile         Enable profiling options (for developers)],
        [ if test "$enable_profile" = "yes" ; then
	    if test -n "$GCC"; then
              CXXFLAGS="$CXXFLAGS -D ENABLE_PROFILE -pg"
              CFLAGS="$CFLAGS -D ENABLE_PROFILE -pg"
            fi
	  fi
        ],)


# Checks for libraries.

######################################################################
# libpcap
######################################################################

AC_ARG_WITH(libpcap,
    [  --with-libpcap=DIR        support libpcap],
    [ with_libpcap="$withval" ],
    [ with_libpcap=$with_libpcap_default ])


libpcap_default_directory="/usr /usr/local /usr/pkg ${prefix}"
libpcap_fail="no"
libpcap_dir="$libpcap_default_directory"
  
if test "$with_libpcap" != "no"; then
  AC_MSG_CHECKING(for libpcap)
  if test "$with_libpcap" = "yes"; then
    libpcap_dir="$libpcap_default_directory";
  elif test -d "$withval"; then
    libpcap_dir="$withval";
  elif test "$with_libpcap" != ""; then
    AC_MSG_ERROR("Invalid Libpcap directory: $with_libpcap")
  fi

  tmp=""
  LIBPCAP_DIR="";
  for i in $libpcap_dir; do
    if test -r $i/include/pcap/pcap.h; then
      LIBPCAP_DIR="$i"
      LIBPCAP_INC_DIR=$i/include/libpcap
    elif test -r $i/include/pcap.h; then
      LIBPCAP_DIR="$i"
      LIBPCAP_INC_DIR=$i/include/
    fi
    tmp="$tmp $i/include/pcap/ $i/include/"
  done

  if test "$LIBPCAP_DIR" = ""; then
    AC_MSG_ERROR("Not found libpcap.h: $tmp")
  fi

  LIBPCAP_LIB_DIR=""
  for i in lib lib/libpcap; do
    tmp="$LIBPCAP_DIR/$i/libpcap.*"
     for j in $tmp; do
       if test -r "$j"; then
         LIBPCAP_LIB_DIR="$LIBPCAP_DIR/$i"
         break
       fi
     done
  done

  if test -z "$LIBPCAP_LIB_DIR"; then
    AC_MSG_ERROR("Not found libpcap.so*: $LIBPCAP_DIR/lib/ $LIBPCAP_DIR/lib/pcap/")
  fi

  AC_MSG_RESULT(yes)
  AC_DEFINE([WITH_LIBPCAP], [1], [With Libpcap])
  LDFLAGS="${LDFLAGS} -L${LIBPCAP_LIB_DIR}"
  CPPFLAGS="${CPPFLAGS} -I${LIBPCAP_INC_DIR}"
  AC_DEFINE_UNQUOTED(WITH_LIBPCAP, 1, support libpcap)
  LIBS="${LIBS} -lpcap"
fi


######################################################                          
# Check for Pcre
######################################################                          

AC_ARG_WITH(pcre,
    [  --with-pcre=DIR        specifying pcre directory],
    [ with_pcre="$withval" ],
    [ with_pcre=$with_pcre_default ])

AC_MSG_CHECKING(for pcre)

pcre_config="`which pcre-config`"

if test -d "$withval"; then
  pcre_config="$withval/bin/pcre-config";
fi

if test ! -x "$pcre_config"; then
  AC_MSG_ERROR("Invalid Pcre directory or no Pcre library: $with_pcre")
fi

AC_MSG_RESULT(yes)
AC_DEFINE([WITH_PCRE], [1], [With Pcre])
LDFLAGS="`${LDFLAGS} $pcre_config --libs`"
CPPFLAGS="${CPPFLAGS} `$pcre_config --cflags`"
AC_DEFINE_UNQUOTED(WITH_PCRE, 1, support pcre)
LIBS="${LIBS} `$pcre_config --libs`"




######################################################################
# pthread
######################################################################


case "`uname`" in
     Darwin | Linux)
     PTHREAD_CFLAGS="-D_THREAD_SAFE -DWITH_PTHREAD"
     PTHREAD_LIBS="-lpthread"
     ;;
esac

CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
LIBS="$LIBS $PTHREAD_LIBS"

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.


AC_OUTPUT
